import { createAction } from 'redux-actions'
import { UPDATEPAGEPERSONDETAIL } from '../types/pagepersondetail'
import { getUserArchiveInfoRequest, getAlike } from '../../../../service/user-archive-controller'
import {
  getZhichuUserInfoByAuth,
  getArchiveInfoRequest
} from '../../../../service/user-info-controller'
import formatdata from '../../../../util/pagemethods/userArchInfo'
import { getOldData } from '../../../../service/member-controller'

export const DOREQUESTPERSONDETAIL = createAction(UPDATEPAGEPERSONDETAIL, payload => {
  return new Promise(resolve => {
    if (payload.isOld) {
      // 获取老平台数据
      getOldData({ archiveId: payload.archiveId }).then(({ data }) => {
        const { entry } = data
        let archiveData = JSON.parse(entry.archiveData)

        let Formatdata = new formatdata(archiveData, entry, true)
        let dataformat = Formatdata.getFormatData()
        resolve({
          networkerror: false,
          showEmpty: false,
          archiveId: entry.archiveId,
          mobile: entry.mobile,
          calyears: entry.age + '岁',
          cmpUserId: entry.userId,
          ...dataformat,
          isOldData: true
        })
      })
    } else {
      Promise.all([
        getUserArchiveInfoRequest({ ...payload }),
        getArchiveInfoRequest({ ...payload }),
        getZhichuUserInfoByAuth({ userId: payload.userId })
      ]).then(([{ data }, { data: { entry: { jobStatus } } }, { data: datain }]) => {
        const userInfo = {
          ...datain.entry,
          jobStatus
        }
        const { networkerror, entry, status, message } = data
        // 所有学历列表
        if (networkerror) {
          resolve({
            networkerror: true
          })
        } else {
          if (status) {
            let Formatdata = new formatdata(entry || {}, userInfo, true)
            let dataformat = Formatdata.getFormatData()
            let wantWork = entry && entry.archive && entry.archive.jobHuntingModels || []
            resolve({
              networkerror: false,
              showEmpty: false,
              archive_id:
                entry.archive &&
                entry.archive.userArchiveModel &&
                entry.archive.userArchiveModel.id,
              personDetail: entry.archive && entry.archive.userArchiveModel,
              ...dataformat,
              wantWork,
              mobile: userInfo.mobile,
              calyears: dataformat.userInfo.age,
              cmpUserId: datain.entry.userId
            })
          } else {
            let Formatdata = new formatdata(entry || {}, userInfo, true)
            let dataformat = Formatdata.getFormatData()
            let wantWork = entry && entry.archive && entry.archive.jobHuntingModels || []
            let calyears = 0
            resolve({
              networkerror: false,
              showEmpty: false,
              archive_id: entry && entry.userArchive ? entry.userArchive.id : '',
              ...dataformat,
              wantWork,
              mobile: userInfo ? userInfo.mobile : '',
              calyears: calyears ? calyears + '岁' : '',
              cmpUserId: datain && datain.entry ? datain.entry.userId : ''
            })
          }
        }
      })
    }
  })
})

export const DOREQUESTPERSONALIKE = createAction(UPDATEPAGEPERSONDETAIL, payload => {
  return new Promise(resolve => {
    getAlike(payload).then(({ data }) => {
      let archiveCollection = data.entry || {}
      resolve({
        collection_id: archiveCollection.id,
        collected: !!archiveCollection.id
      })
    })
  })
})
